package com.pospi.xstore.ui.static_ui;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.pospi.xstore.App;
import com.pospi.xstore.R;
import com.pospi.xstore.adapter.RefundGoodsDetailAdapter;
import com.pospi.xstore.dto.GoodsDto;
import com.pospi.xstore.dto.OrderDto;
import com.pospi.xstore.print.a920.PrintA920;
import com.pospi.xstore.ui.base.BaseActivity;
import com.pospi.xstore.util.Constant;
import com.pospi.xstore.util.Model;
import com.pospi.xstore.util.SaveOrderInfo;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Qiyan on 2016/6/22.
 */
public class FindOrderDetailActivity extends BaseActivity {


    @Bind(R.id.refund_detail_btn)
    Button refundDetailBtn;
    @Bind(R.id.maxno)
    TextView maxno;
    @Bind(R.id.data)
    TextView data;
    @Bind(R.id.cashier_name)
    TextView cashierName;
    @Bind(R.id.sale_type)
    TextView saleType;
    @Bind(R.id.sale_state)
    TextView saleState;
    @Bind(R.id.goods_detail)
    ListView goodslv;
//    @Bind(R.id.pay_type_way)
//    TextView payTypeWay;
//    @Bind(R.id.pay_type_money)
//    TextView payTypeMoney;
    @Bind(R.id.goods_num)
    TextView goodsNum;
    @Bind(R.id.discount_total)
    TextView discountTotal;
    @Bind(R.id.yy_money)
    TextView yyMoney;
    @Bind(R.id.ss_money)
    TextView ssMoney;
    @Bind(R.id.zl_money)
    TextView zlMoney;

    private OrderDto orderDto;
    private RefundGoodsDetailAdapter adapter;
    private List<GoodsDto> goodsDtos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_order_detail);
        setTitle("订单详情");
        ButterKnife.bind(this);
        orderDto = (OrderDto) getIntent().getSerializableExtra(OrderFindActivity.FindOrder);
        init();
    }

    public void init() {
        maxno.setText(String.valueOf(orderDto.getMaxNo()));
        data.setText(orderDto.getCheckoutTime());
        if (orderDto.getOrderType() == Constant.ORDERTYPE_REFUND) {
            saleType.setText("退货");
            saleType.setTextColor(getResources().getColor(R.color.red));
        } else {
            saleType.setText("销售");
        }
        refundDetailBtn.setText("打印小票");
        goodsDtos = SaveOrderInfo.changeToList(orderDto.getOrder_info());
        adapter = new RefundGoodsDetailAdapter(goodsDtos, getApplicationContext());
        goodslv.setAdapter(adapter);
        fixListViewHeight(goodslv);
        cashierName.setText(orderDto.getCashiername());
//        payTypeWay.setText(orderDto.getPayway());
//        payTypeMoney.setText(String.valueOf(orderDto.getYs_money()));
        int num = 0;
        double discount = 0;
        for (int i = 0; i < goodsDtos.size(); i++) {
            num += goodsDtos.get(i).getNum();
            discount += goodsDtos.get(i).getDiscount();
        }
        goodsNum.setText(String.valueOf(num));
        yyMoney.setText(String.valueOf(orderDto.getYs_money()));
        ssMoney.setText(String.valueOf(orderDto.getSs_money()));
        zlMoney.setText(String.valueOf(orderDto.getZl_money()));
        discountTotal.setText(String.valueOf(discount));
    }

    @OnClick(R.id.refund_detail_btn)
    public void onClick() {

        if (App.IsPrinter) {// 如果打印机可用
            if (Build.MODEL.equals(Model.A920)) {
                PrintA920.prnVoucher(getApplicationContext(), orderDto);
            }
        } else {
            showToast("打印功能未开启");
        }


    }

    public void fixListViewHeight(ListView listView) {
        // 如果没有设置数据适配器，则ListView没有子项，返回。
        ListAdapter listAdapter = listView.getAdapter();
        int totalHeight = 0;
        if (listAdapter == null) {
            return;
        }

        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listViewItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listViewItem.measure(0, 0);
            // 计算所有子项的高度和
            totalHeight += listViewItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        // listView.getDividerHeight()获取子项间分隔符的高度
        // params.height设置ListView完全显示需要的高度
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

}
